package com.xkstream.client.ui.live;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.MotionEvent;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.common.CommonDialog;
import com.xkstream.client.entity.AnchorInfo;
import com.xkstream.client.entity.Game;
import com.xkstream.client.entity.ReceiveGift;
import com.xkstream.client.entity.User;
import com.xkstream.client.entity.kt.GiftRankingData;
import com.xkstream.client.presenter.LiveRoomContract;
import com.xkstream.client.presenter.LiveRoomPresenter;
import com.xkstream.client.rx.Rxbus;
import com.xkstream.client.ui.live.fragment.ChatroomFragment;
import com.xkstream.client.ui.live.fragment.ChatroomFragment_lg;
import com.xkstream.client.ui.live.fragment.LiveActionFragment;
import com.xkstream.client.ui.live.fragment.LivePreviewFragment;
import com.xkstream.client.ui.live.fragment.LivePreviewFragment_yuer;
import com.xkstream.client.utils.DeviceUtils;
import com.xkstream.client.utils.LogFileUtil;
import com.xkstream.client.utils.PreferenceUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindArray;
import butterknife.BindString;
import butterknife.ButterKnife;

import static com.xkstream.client.application.Application.mZegoLiveRoom;

/**
 * Created by xiaoyi on 2017-10-9.
 */

public class LiveRoomActivity_V2 extends BaseActivity<LiveRoomContract.Presenter> implements
        LiveRoomContract.View {
    FragmentManager fragmentManager;


    @BindString(R.string.alert_exit)
    String alertExit;

    @BindArray(R.array.stream_lg_count)
    String[] streamLgCountArr;
    List<String> streamLgCoutList;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_live_v2);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        streamLgCoutList = Arrays.asList(streamLgCountArr);

        presenter = new LiveRoomPresenter(this);
        fragmentManager = getSupportFragmentManager();
        initPreviewFragment();
        initChatroomFragment();

    }

    private void initLiveActionFragment() {
        LiveActionFragment fragment = new LiveActionFragment();
        fragmentManager.beginTransaction().add(R.id.frContent, fragment).commit();
    }

    private void initPreviewFragment() {
        Fragment fragment;
        String userName = PreferenceUtils.getString("username");
        if (streamLgCoutList.contains(userName)) {
            fragment = new LivePreviewFragment_yuer();
        } else {
            fragment = new LivePreviewFragment();
            initLiveActionFragment();
        }

        Bundle bundle = new Bundle();
        bundle.putInt("gameType", 4);
        fragment.setArguments(bundle);
        fragmentManager.beginTransaction().add(R.id.frContent, fragment).commit();
    }

    private void initChatroomFragment() {
        Fragment fragment = new ChatroomFragment_lg();
        Bundle bundle = new Bundle();
        bundle.putInt("gameType", 4);
        User user = Application.getUser();
        bundle.putString("chatroomId", user.getAnchor().getNeteaseChatRoomId());
        bundle.putString("nickname", user.getExt_info().getNickname());
        bundle.putString("head", user.getExt_info().getHead_icon());
        fragment.setArguments(bundle);
        fragmentManager.beginTransaction().add(R.id.frContent, fragment).commit();
    }

    public void anchorOff(int status) {

        String userStr = PreferenceUtils.getString(PreferenceUtils.USER);
        User user = new Gson().fromJson(userStr, User.class);
        String id = user.getUser_code();
        int roomId = user.getAnchor().getRoom_id_pk();
        Map<String, Object> params = new HashMap<>();
        params.put("anchor_user_code", id);
        params.put("anchor_status", status);
        params.put("game_type", "2");
        params.put("room_id_pk", roomId + "");
        presenter.anchorOnOff(params);
        LogFileUtil.getDefault().put(TAG, String.format("exit liveroom and call anchorOff() "));
    }

    @Override
    public void onBackPressed() {
        showCloseDialog();
    }

    private void showCloseDialog() {
        CommonDialog dialog = new CommonDialog(this);
        dialog.setTitle(alertExit);
        dialog.setListener(() -> {
            Rxbus.getDefault().post("exit");
            anchorOff(0);

        });
        dialog.show();
    }


    @Override
    public void setPresenter(LiveRoomContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showMuteResult(BaseData data) {
        Rxbus.getDefault().post("dismiss");
        if (data.getCode() == 1) {
            showToast(R.string.mute_success);
        } else {
            showToast(R.string.mute_failed);
        }
    }

    @Override
    public void showSetAdminResult(BaseData data) {
        Rxbus.getDefault().post("dismiss");
        LinkedTreeMap<String, Object> result = (LinkedTreeMap<String, Object>) data.getData();
        if (data.getCode() == 1) {
            if ((double) result.get("operation") == 1.0) {
                showToast(R.string.set_admin_success);
            } else {
                showToast(R.string.remove_admin_success);
            }
        } else {
            if ((double) result.get("operation") == 1.0) {
                showToast(R.string.set_admin_failed);
            } else {
                showToast(R.string.remove_admin_failed);
            }
        }
    }

    @Override
    public void showGiftRecord(BaseData<List<ReceiveGift>> data) {

    }

    @Override
    public void showGiftRanking(BaseData<GiftRankingData> data) {

    }

    @Override
    public void showAnchorDetail(AnchorInfo anchorInfo) {

    }

    @Override
    public void showGameData(Game data, int status) {

    }

    public void setAdmin(Map<String, Object> params) {
        presenter.setChatroomAdmin(params);
    }

    public void chatroomMute(Map<String, Object> params) {
        presenter.chatroomMute(params);
    }

    @Override
    protected void errorTokenAction() {
        finish();
        super.errorTokenAction();
    }
}
